package cc.vant.wechat.model.packet;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.util.CharsetUtil;

/**
 * 前4个字节是类型,接下来是文本
 *
 * @author Vant
 * @version 2018/12/2 上午 12:38
 */
public class SimpleTextPacket implements Packet {
    public static final int TYPE = SIMPLETEXT;
    private String text;

    public SimpleTextPacket() {
    }

    public SimpleTextPacket(String text) {
        this.text = text;
    }

    @Override
    public ByteBuf serialize() {
        ByteBuf buffer = Unpooled.buffer();
        buffer.writeInt(TYPE);
        buffer.writeCharSequence(text, CharsetUtil.UTF_8);
        return buffer;
    }

    @Override
    public void unserialize(ByteBuf data) {
        data.skipBytes(4);
        text = data.readBytes(data.readableBytes()).toString(CharsetUtil.UTF_8);
    }

    @Override
    public int getType() {
        return TYPE;
    }

    public static int getTYPE() {
        return TYPE;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }
}
